#pragma once

#include <stdint.h>
#include <functional>
#include "socketNet.h"
#include "HttpListener.h"
#include "HttpData.h"
#include "HttpHandler.h"



class HttpServer
{
public:
    using HttpMessageCallback = std::function<int(std::uint32_t, stHttpRequest&&)>;
public:
    HttpServer();
    ~HttpServer();
    
public:
    int Start(const char* ip, std::uint16_t port, HttpMessageCallback&& message_cb);
    int Response(std::uint32_t handle, stHttpResponse&& response);

protected: //callback
    virtual int OnConnected(HttpHandler* pHandler);
	virtual int OnClose(HttpHandler* pHandler);
	virtual int OnTimeout(HttpHandler* pHandler);
	virtual int OnMessage(HttpHandler* pHandler, stHttpRequest&& http_data);

protected: 
    virtual int OnAccept(int netfd, struct sockaddr_in* peer);
	virtual HttpHandler* CreateHandler(int netfd, struct sockaddr_in* peer);
	int AcceptNewHandler(HttpHandler* pHandler);

private:	
    HttpListener m_http_listener;
    HttpHandlerMap m_handlerList;
    HttpMessageCallback m_message_cb;
    HttpHandlerSetting m_http_handler_setting;
};
